package com.chenyaowei.happyhub.controller;

import com.chenyaowei.happyhub.mapper.GoodsMapper.GoodIntroMapper;
import com.chenyaowei.happyhub.mapper.GoodsMapper.GoodParameterMapper;
import com.chenyaowei.happyhub.mapper.GoodsMapper.GoodSlideMapper;
import com.chenyaowei.happyhub.mapper.GoodsMapper.GoodVersionMapper;
import com.chenyaowei.happyhub.mapper.IndexNavMapper;
import com.chenyaowei.happyhub.pojo.GoodsPojo.*;
import com.chenyaowei.happyhub.pojo.IndexNav;
import com.chenyaowei.happyhub.service.GoodsService.GoodService;
import com.chenyaowei.happyhub.utils.ArrowUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
@Controller
@RequestMapping("/admin")
public class AdminGoodController {
    @Resource
    GoodService goodService;
    @Resource
    IndexNavMapper indexNavMapper;
    @Resource
    GoodVersionMapper goodVersionMapper;
    @Resource
    GoodParameterMapper goodParameterMapper;
    @Resource
    GoodSlideMapper goodSlideMapper;
    @Resource
    GoodIntroMapper goodIntroMapper;
    ObjectMapper objectMapper=new ObjectMapper();
    String down = "ti-arrow-down";
    String vertical = "ti-arrows-vertical";
    String up = "ti-arrow-up";
    List<Good> goodList = new ArrayList<>();
    PageInfo pageInfo = new PageInfo();
    int pageSize = 10;

    @RequestMapping("/good")
    String adminGood(Model model) throws JsonProcessingException {
        List<IndexNav> indexNavList = indexNavMapper.selectAllType();
        PageInfo pageInfo = goodService.selectGoods(1, pageSize);
        List<Good> goodList = pageInfo.getList();
        model.addAttribute("goodList", goodList);
        model.addAttribute("goodChart", objectMapper.writeValueAsString(goodList));
        model.addAttribute("pageNum", 1);
        model.addAttribute("totalPages", pageInfo.getPages());
        model.addAttribute("indexNavList", indexNavList);
        model.addAttribute("sort", "");
        model.addAttribute("status", "");
        model.addAttribute("type", "");
        model.addAttribute("keyword", "");
        model.addAttribute("metaStatus", vertical);
        model.addAttribute("salesPriceStatus", vertical);
        model.addAttribute("salesVolumeStatus", vertical);
        model.addAttribute("idStatus", vertical);
        return "show";
    }

    @RequestMapping("/Good")
    String searchGood(Model model, String type, String keyword, String sort, String status, String pageNum) throws JsonProcessingException{
        log.debug(type);
        System.out.println(type);
        System.out.println(keyword);
        System.out.println(sort);
        System.out.println(status);
        List<IndexNav> indexNavList = indexNavMapper.selectAllType();
        pageInfo = goodService.searchGood(Integer.parseInt(pageNum), pageSize, type, keyword, sort, status);
        goodList = pageInfo.getList();
        System.out.println(goodList);
        ArrowUtils.setAttribute(model, sort, status);
        model.addAttribute("goodList", goodList);
        model.addAttribute("pageNum", pageNum);
        model.addAttribute("totalPages", pageInfo.getPages());
        model.addAttribute("goodChart", objectMapper.writeValueAsString(goodList));
        model.addAttribute("indexNavList", indexNavList);
        model.addAttribute("keyword", keyword);
        model.addAttribute("sort", sort);
        model.addAttribute("status", status);
        model.addAttribute("type", type);
        return "show";
    }

    @RequestMapping("/updateGood")
    String selectGood(Model model,  String goodId) {
        int ID = Integer.parseInt(goodId);
        List<IndexNav> indexNavList = indexNavMapper.selectAllType();
        Good good=goodService.selectGoodById(ID);
        model.addAttribute("good", good);
        model.addAttribute("indexNavList", indexNavList);
        return "good";
    }
    @RequestMapping("/changeGood")
    String updateGood(Model model,String goodId,String goodHeader,String goodMeta,String goodImage,String goodType,String goodDescription,String isAd,String isTypeAd,String isSlideAd){
        int ID = Integer.parseInt(goodId);
        System.out.println(goodId);
        System.out.println(goodHeader);
        System.out.println(goodMeta);
        System.out.println(goodImage);
        System.out.println(goodDescription);
        System.out.println(isAd);
        System.out.println(isSlideAd);
        System.out.println(isTypeAd);
        goodService.updateGood(goodId,goodHeader,goodMeta,goodImage,goodType,goodDescription,isAd,isTypeAd,isSlideAd);
        List<IndexNav> indexNavList = indexNavMapper.selectAllType();
        Good good=goodService.selectGoodById(ID);
        model.addAttribute("good", good);
        model.addAttribute("indexNavList", indexNavList);
        return "good";
    }
    @RequestMapping("/showVersion")
    String showVersion(Model model,String goodId){
        int ID = Integer.parseInt(goodId);
        List<IndexNav> indexNavList = indexNavMapper.selectAllType();
        model.addAttribute("indexNavList", indexNavList);
        List<GoodVersion> goodVersionList=goodVersionMapper.selectGoodVersionById(ID);
        model.addAttribute("goodVersionList",goodVersionList);
        model.addAttribute("goodId",goodId);
        return "goodVersion";
    }
    @RequestMapping("/deleteGood")
    String deleteGood(Model model,String goodId,String type, String keyword, String sort, String status, String pageNum)throws JsonProcessingException{
        goodService.deleteGood(goodId);
        goodSlideMapper.deleteGoodSlideByGoodId(goodId);
        goodIntroMapper.deleteGoodIntroByGoodId(goodId);
        goodParameterMapper.deleteGoodParameterByGoodId(goodId);
        goodVersionMapper.deleteGoodVersionByGoodId(goodId);
        System.out.println(type);
        System.out.println(keyword);
        System.out.println(sort);
        System.out.println(status);
        List<IndexNav> indexNavList = indexNavMapper.selectAllType();
        pageInfo = goodService.searchGood(Integer.parseInt(pageNum), pageSize, type, keyword, sort, status);
        goodList = pageInfo.getList();
        System.out.println(goodList);
        ArrowUtils.setAttribute(model, sort, status);
        model.addAttribute("goodList", goodList);
        model.addAttribute("pageNum", pageNum);
        model.addAttribute("totalPages", pageInfo.getPages());
        model.addAttribute("goodChart", objectMapper.writeValueAsString(goodList));
        model.addAttribute("indexNavList", indexNavList);
        model.addAttribute("keyword", keyword);
        model.addAttribute("sort", sort);
        model.addAttribute("status", status);
        model.addAttribute("type", type);
        return "show";
    }

}
